package h_10_26;

import java.util.HashMap;
import java.util.HashSet;

/**
 * 1695. 删除子数组的最大得分
 * https://leetcode.cn/problems/maximum-erasure-value/description/
 */
public class Main3 {
    /**
     * Map
     * @param nums
     * @return
     */
    public int maximumUniqueSubarray2(int[] nums) {
        HashMap<Integer,Integer> hash = new HashMap<>();
        int n = nums.length;
        int left = 0;
        int right = 0;
        int ret = 0;
        int sum = 0;
        while(right < n) {
            // 入
            int in = nums[right];
            hash.put(in,hash.getOrDefault(in,0) + 1);
            sum += in;

            // 出
            while(hash.get(in) > 1) {
                int out = nums[left];
                sum -= out;
                hash.put(out,hash.get(out) - 1);
                left++;
            }

            // 更
            ret = Math.max(ret,sum);
            right++;
        }
        return ret;
    }

    /**
     * Set
     * @param nums
     * @return
     */
    public int maximumUniqueSubarray(int[] nums) {
        HashSet<Integer> hash = new HashSet<>();
        int n = nums.length;
        int left = 0;
        int right = 0;
        int ret = 0;
        int sum = 0;
        while(right < n) {
            // 出
            while(hash.contains(nums[right])) {
                int out = nums[left];
                sum -= out;
                hash.remove(out);
                left++;
            }
            // 入
            int in = nums[right];
            hash.add(in);
            sum += in;

            // 更
            ret = Math.max(ret,sum);
            right++;
        }
        return ret;
    }
}
